<?php


namespace app\admin\model;

use think\Model;

// 企业信息管理模型

class Company extends Model
{
    /**
     * 获取列表
     * @param $pageNum
     * @param $numPerPage
     * @param $userName
     * @param $userAccount
     * @param $roleId
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getList($pageNum, $numPerPage, $company_name,$telephone)
    {
        $map = ['is_delete' => 0];
        if ($company_name) {
            $map['company_name'] = ['like', "%{$company_name}%"];
        }
        if ($telephone) {
            $map['telephone'] = ['like', "%{$telephone}%"];
        }
        $numlist = db("company")
                    ->where($map)
                    ->page($pageNum, $numPerPage)
                    ->order('sort, create_time desc')
                    ->select();
        return $numlist;
    }
    // 获取总数
    public function getCount($company_name,$telephone)
    {
        $map = ['is_delete' => 0];
        if ($company_name) {
            $map['company_name'] = ['like', "%{$company_name}%"];
        }
        if ($telephone) {
            $map['telephone'] = ['like', "%{$telephone}%"];
        }
        $count = db("company")->where($map)->count();
        return $count;
    }
    // 执行添加
    public function doAdd_company($data)
    {
        $datas['create_time'] = $data['create_time'] = time();
        $datas['update_time'] = $data['update_time'] = time();
        $datas['is_delete'] = $data['is_delete'] = 0;
        $datas['username'] = $data['username'];
        $datas['password'] = sha1('123456');
        $result = db("company")->insertGetId($data);
        $company = db("company")->where("id", $result)->find();
        $datas['company_id'] = $company['id'];
        $datas['address'] = $company['company_addr'];
        $datas['mobile'] = $company['telephone'];
        $user = db("users")->insert($datas);
        if (!$user) {
            exception('添加失败');
        }
        $codeData = [
            'company_id' => $result,
            'code'       => 1,
            'date'       => date('Y-m-d'),
        ];
        db("subscribe_code")->insert($codeData);
        return $result;
    }
    //执行删除
    public function doDelete($userId)
    {
        $result = db('company')
            ->where('id', $userId)
            ->update(['is_delete'=>1]);
        $result2 = db('users')
            ->where('company_id', $userId)
            ->update(['is_delete'=>1]);
        if (!$result || !$result2) {
            exception('删除失败');
        }
        return $result;
    }
    // 商家信息修改
    public function doModify_company($data)
    {
        $data['update_time'] = time();
        $id = $data['id'];
        unset($data['id']);
        $result = db("company")->where('id',$id)->update($data);
        if(!$result) {
            exception('修改失败');
        }
        return $result;
    }
    /**
     * 状态-启用&禁用
     */
    public function editStatus($data)
    {
        $data['update_time'] = time();
        $result = db("company")->update($data);
        if(!$result){
            exception("操作失败");
        }
        return $result;
    }
    /**
     * 添加可检测车型
     */
    public function doAddVehicle($data)
    {
        $data['update_time'] = time();
        $id = $data['id'];
        unset($data['id']);
        $result = db("company")->where('id', $id)->update($data);
        if(!$result){
            exception("操作失败");
        }
        return $result;
    }
}